package com.stu.springmvc.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.servlet.ModelAndView;

import com.stu.springmvc.entity.item;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

@Controller
public class ItemController implements ApplicationContextAware {

	private ApplicationContext applicationContext;

	@RequestMapping("queryItem")
	public ModelAndView queryItem(HttpServletRequest req) {
		List<item> itemList=new ArrayList<item>();
		
		item item_1=new item();
		item_1.setName("苹果手机");
		item_1.setPrice(5000);
		item_1.setDetail("iphoneX苹果手机!");
		itemList.add(item_1);
		
		item item_2=new item();
		item_2.setName("华为手机");
		item_2.setPrice(6000);
		item_2.setDetail("华为5G网速就是快!");
		itemList.add(item_2);

		ServletContext servletContext = req.getSession().getServletContext();
		String myData = (String) servletContext.getAttribute("myData");

		// 参数顺序为视图逻辑名称，传递给视图的模型对象的名称，传递给视图的模型对象的值
		return new ModelAndView("item-list", "itemList", itemList);
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

	}
}